<?php
namespace Portal\Api\Iadminossms;

use Portal\Common\DataApi as Api;
use PhalApi\Tool;

/**
 * 短信组件
 */
class Tpl extends Api {

    protected function getDataModel() {
        return new \Portal\Model\Sms\Tpl();
    }

    protected function afterTableList($items) {
        $suppliers = \Portal\Model\Sms\Supplier::notorm()->fetchPairs('id', 'sms_supplier_name');

        foreach ($items as &$itRef) {
            $itRef['sms_supplier_name'] = null;
            if (!empty($itRef['sms_supplier_id']) && isset($suppliers[$itRef['sms_supplier_id']])) {
                $itRef['sms_supplier_name'] = $suppliers[$itRef['sms_supplier_id']];
            }
        }

        return $items;
    }

    protected function beforeCreateData($newData) {
        $newData['sms_tpl_key'] = $newData['sms_tpl_type'] . Tool::createRandStr(11);

        $maxId = \Portal\Model\Sms\Tpl::model()->max('id > 0', 'id');
        $newData['sms_tpl_key'] = 'T' . (intval($maxId) + 1);

        $newData['add_time'] = date('Y-m-d H:i:s');
        return $newData;
    }
}




